/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.cms.web;

import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.fyx.cms.ejb.CmsLocal;
import org.fyx.cms.entity.CmsArticle;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.mula.controll.MulaLanguageFacadeLocal;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.mula.entity.MulaLanguage;
import org.fyx.secure.ejb.SecureLocal;
import org.fyx.web.context.WebContext;
import org.fyx.web.context.WebPage;

/**
 *
 * @author ydenek
 */
@ManagedBean
@SessionScoped
public class CmsArticleEdit {

    @EJB
    private MulaLocal mula;
    @EJB
    private LinkerLocal linker;
    @EJB
    private CmsLocal cms;
    @EJB
    private MulaLanguageFacadeLocal mulaLanguageFacase;
    @EJB
    private SecureLocal secure;
    private CmsArticle article = new CmsArticle();

    /** Creates a new instance of CmsArticle */
    public CmsArticleEdit() {
    }

    public CmsArticle getArticle() {
        return article;
    }

    public void setArticle(CmsArticle article) {
        this.article = article;
    }

    public void save() {
        cms.addArticle(this.article);
        linker.recreateMenuModel();
        FacesMessage msg = new FacesMessage(mula.get("Article was created."),
                mula.get("Congratulations."));
        WebContext.getMessages().add(msg);
        WebPage.redirect(linker.getLink("cms/list.xhtml"));
    }

    public void edit() {
        this.article.setTimeEditation(new Date());
        cms.editArticle(this.article);
        FacesMessage msg = new FacesMessage(mula.get("Article was changed."),
                mula.get("Congratulations."));
        WebContext.getMessages().add(msg);
        WebPage.redirect(linker.getLink("cms/list.xhtml"));
    }

    public void initArticle(int idArticle) {
        if (idArticle == 0) {
            FacesMessage msg = new FacesMessage(mula.get("Select the article."),
                    mula.get("You must select article."));
            WebContext.getMessages().add(msg);
            WebPage.redirect(linker.getLink("cms/list.xhtml"));
            return;
        }
        this.article = cms.getArticleById(idArticle);
    }

    public void initArticle() {
        this.article = new CmsArticle();
    }

    public List<MulaLanguage> getLanguages() {
        return mulaLanguageFacase.findAll();
    }
}
